package cn.platform.controller;

import cn.platform.entity.SchAnnounce;
import cn.platform.request.SchAnnounceQueryDTO;
import cn.platform.response.PageResult;
import cn.platform.response.Result;
import cn.platform.response.SchAnnounceVO;
import cn.platform.service.SchAnnounceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/api/announcements")
public class SchAnnounceController {
    @Autowired
    SchAnnounceService schAnnounceService;

    /**
     * 实现分页条件查询操作
     * @param announcementQueryDTO
     * @return
     * @author oyq
     */
    @GetMapping
    public Result page(SchAnnounceQueryDTO announcementQueryDTO){
        log.info("分页查询公告信息");
        PageResult<SchAnnounceVO> pageResult = schAnnounceService.page(announcementQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 查看公告详情
     * @param id
     * @return
     * @author oyq
     */
    @GetMapping("/{id}")
    public Result detail(@PathVariable Integer id){
        log.info("查看公告详情");
        SchAnnounceVO schAnnounceVo = schAnnounceService.detail(id);
        return Result.success(schAnnounceVo);
    }

    /**
     * 新增公告
     * @param schAnnounce
     * @return
     * @author oyq
     */
    @PostMapping()
    public Result save(@RequestBody SchAnnounce schAnnounce){
        log.info("新增公告");
        schAnnounceService.save(schAnnounce);
        return Result.success();
    }

    /**
     * 更新公告
     * @param id
     * @param schAnnounce
     * @return
     * @author oyq
     */
    @PutMapping("/{id}")
    public Result update(@PathVariable Integer id, @RequestBody SchAnnounce schAnnounce){
        log.info("更新公告的ID{}，数据{}",id,schAnnounce);
        schAnnounce.setId(id);
        schAnnounceService.update(schAnnounce);
        return Result.success();
    }

    /**
     * 删除单条公告记录
     * @param id
     * @return
     * @author oyq
     */
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id){
        log.info("删除公告{}",id);
        schAnnounceService.delete(id);
        return Result.success();
    }

    /**
     * 批量删除公告
     * @param ids
     * @return
     * @author oyq
     */
    @PostMapping("/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids){
        log.info("批量删除：{}",ids);
        schAnnounceService.deleteBatch(ids);
        return Result.success();
    }

    /**
     * 撤销或置顶公告
     * @param schAnnounce
     * @return
     * @author oyq
     */
    @PostMapping("/top")
    public Result toggleTop(@RequestBody SchAnnounce schAnnounce){
        log.info("置顶或撤销置顶{}",schAnnounce);
        schAnnounceService.toggleTop(schAnnounce);
        return Result.success();
    }

    /**
     * 更改公告状态
     * @param schAnnounce
     * @return
     * @author oyq
     */
    @PostMapping("/status")
    public Result toggleStatus(@RequestBody SchAnnounce schAnnounce){
        log.info("更改公告状态");
        schAnnounceService.toggleStatus(schAnnounce);
        return Result.success();
    }

}
